package com.aabte.mp.metaObjectHandler;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

public class MyMetaObjectHandler extends MetaObjectHandler {

    private static final String AUTO_FILL_FILED_NAME = "gender";
    private static final Integer AUTO_FILL_FILED_VALUE = 0;

    @Override
    public void insertFill(MetaObject metaObject) {
        fill(metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        fill(metaObject);
    }

    private void fill(MetaObject metaObject){
        Object fieldValue = getFieldValByName(AUTO_FILL_FILED_NAME, metaObject);
        if (null == fieldValue) {
            // 字段为null，设置自动填充值
            System.out.println("修改操作，满足自动填操作.");
            setFieldValByName(AUTO_FILL_FILED_NAME, AUTO_FILL_FILED_VALUE, metaObject);
        }
    }

}
